//
//  NSDate+Extension.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/22.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import Foundation

private let xl_shareDateFormatter = NSDateFormatter()
/// 当前日历对象
private let xl_shareCalendar = NSCalendar.currentCalendar()

extension NSDate{
    /// 将新浪日期格式转换成日期
    ///
    /// - parameter create_at: Fri Apr 22 13:00:03 +0800 2016
    ///
    /// - returns: 日期对象
    class func xl_sinaDate(create_at: String?) -> NSDate? {
        
        
        guard let create_at = create_at else{
            
            return nil
        }
        
       
        
        xl_shareDateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        
        xl_shareDateFormatter.locale = NSLocale(localeIdentifier: "en")
        
        
        return xl_shareDateFormatter.dateFromString(create_at)
        
    
    }
    
    /*
    刚刚(一分钟内)
    X分钟前(一小时内)
    X小时前(当天)
    昨天 HH:mm(昨天)
    MM-dd HH:mm(一年内)
    yyyy-MM-dd HH:mm(更早期)
    */
    
/// 计算形属性
    var xl_descriptiondate: String {
        /// <#Description#>
      if xl_shareCalendar.isDateInToday(self){
            
        let  oneMinutes = Int(NSDate().timeIntervalSinceDate(self))
            
            if oneMinutes < 60 {
                
                return "刚刚"
            }else if oneMinutes < 60 * 60 {
                
                return "\(oneMinutes/60)分钟前"
            }else {
                
                return "\(oneMinutes/3600)小时前"
            }
            
        }
        
        var format = " HH:mm"
        if xl_shareCalendar.isDateInYesterday(self){
            
            format = "昨天" + format
        }else{
            format = "MM-dd" + format
            
            let thisYear = xl_shareCalendar.component(.Year, fromDate: NSDate())
            let year = xl_shareCalendar.component(.Year, fromDate: self)
            
            if year != thisYear{
                
                format = "yyyy-" + format
            }
            
        }
        
        xl_shareDateFormatter.dateFormat = format
        xl_shareDateFormatter.locale = NSLocale(localeIdentifier: "en")
        
        return xl_shareDateFormatter.stringFromDate(self)
    }
    
}